package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.dto.SubjectLessonDto;
import com.dingreading.cloud.admin.entity.SubjectLessonApply;
import com.dingreading.cloud.admin.entity.Subjects;
import com.dingreading.cloud.admin.service.SubjectLessonApplyService;
import com.dingreading.cloud.admin.service.SubjectLessonService;
import com.dingreading.cloud.admin.service.SubjectsService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.TimeUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/subjectLessonApply")
@Api(tags = "总部-课次信息修改申请（上传等功能在备课服务）")
public class SubjectLessonApplyController {

    @Resource
    private SubjectsService subjectsService;
    @Resource
    private SubjectLessonService subjectLessonService;
    @Resource
    private SubjectLessonApplyService subjectLessonApplyService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "活动名") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "课次id") @RequestParam(value = "lessonId", required = false) Long lessonId,
            @ApiParam(value = "状态 0.待审核 1.通过 -1.拒绝") @RequestParam(value = "status", required = false) Integer status
    ) {
        Page<SubjectLessonApply> page = this.subjectLessonApplyService.pageList(pageUtil, keyword, lessonId, status);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "新增/修改")
    @PostMapping(value = "/newBean")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "课次id") @RequestParam(value = "lessonId") Long lessonId,
            @ApiParam(value = "课次名") @RequestParam(value = "lessonName", required = false) String lessonName,
            @ApiParam(value = "活动指导步骤url") @RequestParam(value = "teachPlanUrl", required = false) String teachPlanUrl,
            @ApiParam(value = "课件html地址") @RequestParam(value = "htmlUrl", required = false) String htmlUrl,
            @ApiParam(value = "资料地址") @RequestParam(value = "dataUrl", required = false) String dataUrl
    ) {
        SubjectLessonApply data = null;
        if (id == null || id <= 0) {
            if (StringUtils.isBlank(lessonName) && StringUtils.isBlank(teachPlanUrl) && StringUtils.isBlank(dataUrl) && StringUtils.isBlank(htmlUrl))
                return R.fail("请先上传要修改的信息");

            boolean b = subjectLessonApplyService.haveLessonId(lessonId);
            if (b)
                return R.fail("课次下有待审核的申请记录，不能操作");

            SubjectLessonDto lesson = subjectLessonService.dtoRelationById(lessonId);
            if (lesson == null)
                return R.fail("没有查询到课次");

            // 新增
            Subjects mainSubjects = subjectsService.getById(lesson.getSubjectMainId());
            if (mainSubjects == null)
                return R.fail("主学科获取失败");
            if (StringUtils.isBlank(mainSubjects.getSubjectDescribe()))
                return R.fail("主学科标识不能为空");

            data = new SubjectLessonApply();
            data.setSubjectMainId(lesson.getSubjectMainId());
            data.setSubjectId(lesson.getSubjectId());
            data.setLessonId(lessonId);
            data.setStatus(0);
            data.setOrigLessonName(lesson.getLessonName());
            data.setSubjectDescribe(mainSubjects.getSubjectDescribe());
            String levelName = "";
            if (CollectionUtils.isNotEmpty(lesson.getSubjectList())) {
                levelName = lesson.getSubjectList().stream().collect(Collectors.joining(" / "));
            }
            data.setLevelName(levelName);
            data.setAddStaffUid(headStaffUid);
            data.setAddStaffName(headStaffName);
        } else {
            // 修改
            data = subjectLessonApplyService.getById(id);
            if (data == null)
                return R.fail("申请记录获取失败");
            if (data.getStatus().equals(1))
                return R.fail("申请已通过，不能操作");
            if (TimeUtil.moreThanDay(data.getAddTime(), 30))
                return R.fail("已提交超过30天，不能操作");
            long maxId = subjectLessonApplyService.getMaxId(data.getLessonId());
            if (!id.equals(maxId))
                return R.fail("记录已过期，不能操作");
            if (data.getStatus().equals(-1))
                data.setStatus(0);
            data.setLastStaffUid(headStaffUid);
            data.setLastStaffName(headStaffName);
        }

        if (StringUtils.isNotBlank(lessonName))
            data.setLessonName(lessonName);
        data.setTeachPlanUrl(teachPlanUrl);
        data.setHtmlUrl(htmlUrl);
        data.setDataUrl(dataUrl);
        subjectLessonApplyService.saveOrUpdate(data);

        return R.ok();
    }


    @ApiOperation(value = "根据ID获取信息")
    @PostMapping(value = "/getById", produces = "application/json; charset=UTF-8")
    public R<Object> getById(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        SubjectLessonApply data = subjectLessonApplyService.getById(id);
        if (data == null)
            return R.fail("课次修改申请获取失败");
        return R.ok(data);
    }


    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        SubjectLessonApply data = subjectLessonApplyService.getById(id);
        if (data == null)
            return R.fail("课次申请记录获取失败");
        if (data.getStatus().equals(1))
            return R.fail("审核已通过，不能操作");

        long l = subjectLessonApplyService.dropBean(id);
        if (l <= 0)
            return R.failDel();

        return R.ok();
    }

}
